package com.java.code.jdbc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.code.domain.Page;
import com.java.code.domain.Todo;
import com.java.code.jdbc.dao.TodoMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyBatisTest {
    /**
     * 测试1级缓存和2级缓存
     * mybatis-config.xml 设置 <setting name="cacheEnabled" value="true"/>
     * TodoMapper.xml 设置 <cache />
     * select 设置 useCache="false" 每次查询都是实时的数据
     * update 设置 flushCache=true 每次更新后都会刷新缓存
     *
     * @throws Exception
     */
    @Test
    public void test() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        TodoMapper mapper = session.getMapper(TodoMapper.class);
        //第一次查找对象
        Todo todo1 = mapper.selectById(1);
        //第二次查找对象
        Todo todo2 = mapper.selectById(1);
        System.out.println(todo1);
        System.out.println("todo1 == todo2 ? " + (todo1 == todo2));

        //这里需要commit一下,否则不会
        session.commit();
        //开启另一个SqlSession
        SqlSession session2 = sqlSessionFactory.openSession();
        TodoMapper mapper2 = session2.getMapper(TodoMapper.class);
        System.out.println("session == session2 ? " + (session == session2));
        Todo todo3 = mapper2.selectById(1);
        System.out.println("todo1 == todo3 ? " + (todo1 == todo3));
    }

    /**
     * 测试拦截器实现分页
     *
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        TodoMapper mapper = session.getMapper(TodoMapper.class);
        Page<Todo> page = new Page<>();
        List<Todo> todos = mapper.selectListByPage(page);
        session.commit();
    }

    /**
     * RowBounds实现分页
     *
     * @throws Exception
     */
    @Test
    public void test2() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        TodoMapper mapper = session.getMapper(TodoMapper.class);
        List<Todo> todos = mapper.selectListByRowBounds(new RowBounds(0, 5));
        session.commit();
    }

    /**
     * PageHelper实现分页
     *
     * @throws Exception
     */
    @Test
    public void test3() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        TodoMapper mapper = session.getMapper(TodoMapper.class);

        PageHelper.startPage(0, 4);
        List<Todo> todos = mapper.selectListByPageHelper();
        PageInfo<Todo> pageInfo = new PageInfo<Todo>(todos);
        session.commit();
    }
}
